package cn.paging.service.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.paging.bean.PageBean;
import cn.paging.dao.MemberDao;
import cn.paging.model.Member;
import cn.paging.service.MemberService;
import cn.paging.util.HibernateUtils;

public class MemberServiceImpl implements MemberService
{

	private MemberDao memberDao;

	public void setMemberDao(MemberDao memberDao)
	{
		this.memberDao = memberDao;
	}

	/** */
	/**
	 * query for paging
	 * 
	 * @param currentPage 
	 *            
	 * @param pageSize (the quantity of per page)
	 *            
	 * @return page information
	 */
	public PageBean queryForPage(int pageSize, int page)
	{

		final String hql = "from Member ";
		int allRow = memberDao.getAllRowCount(hql); //all records
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // all pages
		final int offset = PageBean.countOffset(pageSize, page); // the start page
		final int length = pageSize; // quantity of records in a page 
		final int currentPage = PageBean.countCurrentPage(page);
		List<Member> list = memberDao.queryForPage(hql,offset,length); 

		// keep the page information in pagaBean
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	public void delete(int id)
	{
		memberDao.delete(id);
	}

	public void update(Object o)
	{
		memberDao.update(o);
	}

	public void add(Object o)
	{
		memberDao.add(o);
		
	}
	public Object edit(int id)
	{
		
		return memberDao.edit(id);
	}

	
}
